use std::fs::File;
use std::io::{BufRead, BufReader, Error};
use regex::Regex;
use crate::cli;

pub fn search<'a>(args: cli::Cli) -> Result<Vec<String>, Error> {
    //Open File
    let file_data= File::open(args.file).unwrap();
    //Find a pattern matches
    find_patterns(args.pattern.as_str(),file_data)
}

fn find_patterns(pattern: &str, file: File) -> Result<Vec<String>,Error> {
    let re = Regex::new(pattern).unwrap();
    let mut matches: Vec<String> = vec!();
    for line in BufReader::new(file).lines() {
        let line = line?;
        if let Some(_) = re.find(&line) {
            matches.push(line);
        }
    }
    Ok(matches)
}